/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.subcomponents;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.admin.client.utils.AdminUtils;
import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.callback.SubComponentCallback;
import com.elf.gwt.common.client.model.AbbreviatedProductModel;
import com.elf.gwt.common.client.model.DetailedProductModel;
import com.elf.gwt.common.client.model.EditProductXsellMetaModel;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;

/**
 *  
 */
public class ProductXSellSubComponent extends AdminSubComponent
{
	protected EditProductXsellMetaModel metaModel;
	protected DetailedProductModel productModel;
	protected Grid grid;
	protected List<CheckBox> xsellOutCheckBoxes;
	protected List<CheckBox> xsellInCheckBoxes;

	/**
	 * Constructor
	 */
	public ProductXSellSubComponent(EditProductXsellMetaModel metaModel, AdminComponentController controller, String handle, SubComponentCallback callback)
	{
		super(controller, handle, null, callback);
		this.metaModel = metaModel;
		xsellOutCheckBoxes = new ArrayList<CheckBox>();
		xsellInCheckBoxes = new ArrayList<CheckBox>();
	}

	public List<RequestParameter> getAdditionalRequestParameters()
	{
		List<AbbreviatedProductModel> products = metaModel.getProducts();
		Iterator<AbbreviatedProductModel> iterProducts = products.iterator();
		Iterator<CheckBox> iterXsellOutCheckBoxes = xsellOutCheckBoxes.iterator();
		Iterator<CheckBox> iterXsellInCheckBoxes = xsellInCheckBoxes.iterator();
		List<RequestParameter> requestParameters = new ArrayList<RequestParameter>();
		String xsellOutIds = "";
		String xsellInIds = "";
		while(iterProducts.hasNext())
		{
			AbbreviatedProductModel model = iterProducts.next();
			CheckBox xsellOutCheckBox = iterXsellOutCheckBoxes.next();
			CheckBox xsellInCheckBox = iterXsellInCheckBoxes.next();
			if (xsellOutCheckBox.getValue())
				xsellOutIds += model.getId() + ",";
			if (xsellInCheckBox.getValue())
				xsellInIds += model.getId() + ",";
		}
		if (xsellOutIds.length() > 0) {
			xsellOutIds = xsellOutIds.substring(0, xsellOutIds.length() -1);
			requestParameters.add(new RequestParameter("out_xsell_ids", xsellOutIds));
		}
		if (xsellInIds.length() > 0) {
			xsellInIds = xsellInIds.substring(0, xsellInIds.length() -1);
			requestParameters.add(new RequestParameter("in_xsell_ids", xsellInIds));
		}

		return requestParameters;
	}

	public void build()
	{
		xsellOutCheckBoxes.clear();
		xsellInCheckBoxes.clear();
		List<AbbreviatedProductModel> products = metaModel.getProducts();
		grid = new Grid(products.size()+1, 4);
		grid.setStyleName(handle + "XSell");

		Iterator<AbbreviatedProductModel> iterProducts = products.iterator();
		int row = 0;
		grid.setText(0,0, "Image");
		grid.setText(0,1, "Product Name");
		grid.setText(0,2, "Xsell (To)");
		grid.setText(0,3, "Xsell (From)");
		row++;
		while(iterProducts.hasNext())
		{
			int k = row % 2;
			String rowStyle = "Even";
			if (k > 0) rowStyle = "Odd";
			grid.getRowFormatter().addStyleName(row, rowStyle);

			AbbreviatedProductModel model = iterProducts.next();
			Image image = new Image();
			image.setUrl(AdminUtils.getImageProductsDirectory() + model.getImage());
			String[] imageSize = model.getImageSize();
			image.setSize(imageSize[0], imageSize[1]);
			grid.setWidget(row, 0, image);
			grid.setText(row, 1, model.getName());
			CheckBox xsellOutCheckBox = new CheckBox();
			Iterator<Integer> iterXsellOut = metaModel.getOutXSells().iterator();
			boolean match = false;
			while(iterXsellOut.hasNext())
			{
				int id = iterXsellOut.next().intValue();
				if (id == model.getId()) {
					match = true;
					break;
				}
			}
			xsellOutCheckBox.setValue(match);
			xsellOutCheckBoxes.add(xsellOutCheckBox);
			grid.setWidget(row, 2, xsellOutCheckBox);
			CheckBox xsellInCheckBox = new CheckBox();
			Iterator<Integer> iterXsellIn = metaModel.getInXSells().iterator();
			match = false;
			while(iterXsellIn.hasNext())
			{
				int id = iterXsellIn.next().intValue();
				if (id == model.getId()) {
					match = true;
					break;
				}
			}
			xsellInCheckBox.setValue(match);
			xsellInCheckBoxes.add(xsellInCheckBox);
			grid.setWidget(row, 3, xsellInCheckBox);

			row++;
		}
		add(grid);
	}

}
